<template>
	<view>
		<wrapBgImgVue
			bgImg="goods/goods_top.png"
		/>
		
		<navigationBarVue
			:bgImgType="1"
			navigationTitle="免费入会 尊享好礼"
			@onTotalHeight="onTotalHeight"
		/>
		
		<view class="wrap paddingBottom"
			:style="'padding-top: calc(' + (topHeight + 10) + 'px) !important;'"
			v-if="dataInfo"
		>
			<view class="shop-info flex align-center">
				<image
					class="shop-logo"
					mode="aspectFill"
					:src="$wanlshop.oss(dataInfo.avatar)"
				/>
				
				<view class="shop-name">
					{{ dataInfo.shopname }}
				</view>
			</view>
			
			<view class="shop-vip-explain">
				<view class="explain-title">
					加入会员尊享好礼
				</view>
				
				<view class="explain-list flex">
					<block v-for="(item, index) in dataInfo.rights" :key="index">
						<view class="explain-item flex column align-center just-center" @click="onExplain(item)">
							<image
								class="explain-img"
								mode="aspectFill"
								:src="$wanlshop.oss(item.image)"
							/>
							
							<view class="explain-name">
								{{ item.title }}
							</view>
						</view>
					</block>
				</view>
			</view>
			
			<view class="input-box flex column">
				<view class="input-item flex align-center">
					<input
						class="input"
						placeholder="输入手机号"
						type="tel"
						placeholder-class="place"
						maxlength="11"
						v-model="mobile" 
					/>
				</view>
				
				<view class="input-item flex align-center">
					<input
						class="input"
						placeholder="输入验证码"
						type="number"
						maxlength="6"
						placeholder-class="place"
						v-model="mobileCode" 
					/>
					
					<view class="get-code">
						{{ codeTips }}
					</view>
				</view>
			</view>
		</view>
		
		<wrapFooterVue>
			<view class="wrap-bottom-bg">
				<view class="btn" @click.stop="onJoin">
					免费加入
				</view>
			</view>
		</wrapFooterVue>
		
		<vipExplainPopupVue
			:isShow.sync="isShowExplain"
			:content="explainContent"
		/>
	</view>
</template>

<script>
	import {
		CodeGetShopInfo,
		sendSMS,
		bindShopMember,
	} from "@/utils/API.js";
	import navigationBarVue from "../components/navigationBar/navigationBar.vue";
	import wrapBgImgVue from "../components/navigationBar/bgImg.vue";
	import wrapFooterVue from "../components/footer/footer.vue";
	import vipExplainPopupVue from "../components/popupContent/vipExplainPopup.vue";
	
	export default {
		components: {
			wrapBgImgVue,
			navigationBarVue,
			wrapFooterVue,
			vipExplainPopupVue,
		},
		data() {
			return {
				topHeight: 0, //顶部高度
				
				shopCode: "", //店铺code
				dataInfo: null, //店铺信息
				
				mobile: "", //手机号
				mobileCode: "", //手机号验证码
				
				codeTips: "获取验证码", //验证码文本
				countdownInterval: null,
				countTime: 60,
				hasCode: false, //是否已经获取验证码了
				
				isShowExplain: false,
				explainContent: "",
			}
		},
		onLoad(options) {
			if (options.hasOwnProperty("q")) {
				const url = decodeURIComponent(options.q);
				var obj = {};
				url.replace(/([^?&=]+)=([^&]+)/g, (_, k, v) => (obj[k] = v));
				
				this.shopCode = obj.code;
				
				this.getData();
			}
		},
		onUnload() {
			if (this.countdownInterval !== null) {
				this.onStopInterval();
			}
		},
		methods: {
			/**
			 * 顶部高度
			 */
			onTotalHeight(height) {
				this.topHeight = height;
			},
			/**
			 * 权益
			 */
			onExplain(item) {
				this.explainContent = item.content;
				
				this.isShowExplain = true;
			},
			/**
			 * 获取验证码
			 */
			onGetCode() {
				if (this.mobile === "") {
					this.$st.show("请输入手机号")
					return;
				}
				if (this.hasCode === true) {
					return;
				}
				this.hasCode = true;
				
				this.$throttle(() => {
					sendSMS({
						mobile: this.mobile,
						event: "blindshop"
					}).then(res => {
						this.$st.success("发送成功");
						this.startCountdown();
					}).catch(() => {
						this.hasCode = false;
					})
				})
			},
			/**
			 * 倒计时
			 */
			startCountdown() {
				this.countTime = 60;
				this.countdownInterval = setInterval(() => {
					if (this.countTime > 0) {
						this.countTime--;
						this.codeTips = this.countTime + "s后获取";
					} else {
						this.onStopInterval();
						this.codeTips = "获取验证码";
						this.hasCode = false;
					}
				}, 1000);
			},
			/**
			 * 结束倒计时
			 */
			onStopInterval() {
				clearInterval(this.countdownInterval);
			},
			/**
			 * 加入会员
			 */
			onJoin() {
				if (this.mobile === "") {
					this.$st.show("请输入手机号")
					return;
				}
				if (this.mobileCode === "") {
					this.$st.show("请输入验证码")
					return;
				}
				
				this.$throttle(() => {
					bindShopMember({
						mobile: this.mobile,
						code: this.mobileCode,
						shop_id: this.dataInfo.id,
					}).then(res => {
						this.$st.success("申请成功", () => {
							uni.navigateBack();
						})
					})
				})
			},
			/**
			 * 网络请求
			 */
			getData() {
				CodeGetShopInfo(this.shopCode).then(res => {
					this.dataInfo = res.data;
				})
			},
		},
	}
</script>

<style lang="scss">
	.wrap {
		--bottom-height: 150rpx;
		padding: 30rpx;
		box-sizing: border-box;
		
		.shop-info {
			width: 100%;
			background-color: #84BD00;
			border-radius: 20rpx;
			display: flex;
			align-items: center;
			padding: 32rpx 27rpx;
			box-sizing: border-box;
			gap: 24rpx;
			
			.shop-logo {
				width: 84rpx;
				height: 84rpx;
				border-radius: 16rpx;
			}
			
			.shop-name {
				font-size: 33rpx;
				color: #FFFFFF;
			}
		}
		
		.shop-vip-explain {
			margin-top: 50rpx;
			width: 100%;
			overflow: hidden;
			
			.explain-title {
				font-size: 34rpx;
				font-weight: bold;
				color: #3D3D3D;
			}
			
			.explain-list {
				margin-top: 70rpx;
				width: 100%;
				overflow: hidden;
				flex-wrap: wrap;
				gap: 30rpx;
				
				.explain-item {
					gap: 20rpx;
					overflow: hidden;
					
					.explain-img {
						width: 100rpx;
						height: 100rpx;
						border-radius: 50%;
					}
					
					.explain-name {
						font-size: 26rpx;
						color: #3D3D3D;
					}
				}
			}
		}
		
		.input-box {
			margin-top: 60rpx;
			width: 100%;
			overflow: hidden;
			gap: 40rpx;
			
			.input-item {
				width: 100%;
				height: 90rpx;
				border-radius: 50rpx;
				border: 2rpx solid #4D86EF;
				padding: 0rpx 40rpx;
				box-sizing: border-box;
				gap: 20rpx;
				
				.input {
					flex: 1;
					height: 60rpx;
					overflow: hidden;
					font-size: 26rpx;
					color: #3D3D3D;
				}
				
				.get-code {
					color: #4D86EF;
					font-size: 26rpx;
				}
				
				place {
					color: #999999;
					font-size: 26rpx;
				}
			}
		}
		
	}
	
	.wrap-bottom-bg {
		width: 100vw;
		height: 120rpx;
		padding: 0rpx 38rpx;
		box-sizing: border-box;
		
		.btn {
			width: 100%;
			height: 82rpx;
			line-height: 82rpx;
			text-align: center;
			border-radius: 50rpx;
			color: #fff;
			background-color: #84BD00;
			font-size: 30rpx;
		}
	}
</style>